package com.surfacing.rcp.client.controller;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.resource.ImageDescriptor;

import com.surfacing.rcp.core.net.NetWrapper;
import com.surfacing.rcp.model.QueryFilter;
import com.surfacing.rcp.model.common.IFilterTypes;
import com.surfacing.rcp.model.entity.ProtokollNr;
import com.surfacing.rcp.model.factory.ImageFactory;

/**
 * This action sends a listing request to the server to update the displayed
 * data
 * 
 * @author Michael
 */
public class RefreshViewAction extends Action {
	// the date, if needed for the query
	private final String modelId;
	private long date;
	private int page;

	/**
	 * Default class constructor to request a refresh
	 * 
	 * @param modelId
	 *            the id of the model to request the update
	 */
	public RefreshViewAction(String modelId) {
		this.modelId = modelId;
	}

	/**
	 * Default class constructor to request a filtered request
	 * 
	 * @param modelId
	 *            the id of the model to request the update
	 * @param date
	 *            the date period to limit the data
	 */
	public RefreshViewAction(String modelId, long date) {
		this.modelId = modelId;
		this.date = date;
	}

	public RefreshViewAction(String modelId, int page) {
		this.modelId = modelId;
		this.page = page;
	}

	/**
	 * Runs the action
	 */
	@Override
	public void run() {
		NetWrapper.getDefault().sendNewProtokollNr(ProtokollNr.ID);
		// filtered by date?
		if (date > 0) {
			QueryFilter filter = new QueryFilter(IFilterTypes.DATE_FILTER,
					String.valueOf(date));
			NetWrapper.getDefault().requestListing(modelId, filter);
		} else if (page > 0) {
			QueryFilter filter = new QueryFilter(IFilterTypes.COUNT_FILTER,
					String.valueOf(page));
			NetWrapper.getDefault().requestListing(modelId, filter);
		} else
			NetWrapper.getDefault().requestListing(modelId, null);
	}

	/**
	 * Retruns the text to show in the toolbar
	 * 
	 * @return the text to render
	 */
	@Override
	public String getText() {
		return "Ansicht aktualisieren";
	}

	/**
	 * Returns the image to use for this action.
	 * 
	 * @return the image to use
	 */
	@Override
	public ImageDescriptor getImageDescriptor() {
		return ImageFactory.getInstance().getRegisteredImageDescriptor(
				"resource.refresh");
	}
}
